import time
import threading

class MyThread(threading.Thread):
    def __init__(self, input, output, lock):
        super(MyThread, self).__init__()
        self.input = input
        self.output = output
        self.lock = lock # 传入的lock对象

    def run(self):
        self.lock.acquire() # 获得lock对象，lock状态变为locked，并且阻塞其他线程获取lock对象
        for line in self.input.readlines():
            time.sleep(1) # 模拟耗时操作
            self.output.write(line)
        self.lock.release() # 释放lock对象
        print('Thread Done')

def main():
    lock = threading.Lock() # 创建lock对象
    txt1 = open('1.txt', 'r')
    txt2 = open('2.txt', 'r')
    txt3 = open('3.txt', 'a')
    t1 = MyThread(txt1, txt3, lock)
    t2 = MyThread(txt2, txt3, lock)
    t1.start()
    t2.start()
    t1.join()
    t2.join()

main()